/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: 异构集合通信CPU侧event
 */

#ifndef ESCHED_NOTIFY_H
#define ESCHED_NOTIFY_H

#include <atomic>
#include "notify_base.h"

namespace hccl {
// 订阅事件相关的参数
struct CpuSendInfo {
    int32_t devId;
    uint32_t grpId;
    uint32_t threadId;
    uint64_t eventBitMap;
};

class EschedNotify : public NotifyBase {
public:
    EschedNotify(NotifyType notifyType);
    EschedNotify(NotifyType notifyType, HcclNotifyInfo notifyInfo_);
    ~EschedNotify();

    HcclResult Alloc() override;
    HcclResult Open() override;
    HcclResult Close() override;

    HcclResult Wait(Stream& stream, HcclDispatcher dispatcher, s32 stage, u32 timeOut) override;
    HcclResult Post(Stream& stream, HcclDispatcher dispatcher, s32 stage) override;

    HcclResult Wait(Stream& stream, HcclDispatcher dispatcher, s32 stage, u32 timeOut,
        u32 userRank, u32 remoteUserRank) override;
    HcclResult Post(Stream& stream, HcclDispatcher dispatcher, s32 stage,
        u32 remoteUserRank) override;

    HcclResult SetIpc() override;
    HcclResult Grant(s64 recvId) override;
    HcclResult Destroy() override;
    void Break() override;

    static void SetEventIdAndTid(const u32 &eventId, const u32 &tid)
    {
        eventId_ = eventId;
        initialThreadId_ = tid;
        return;
    }

    static void ThreadIdQueInit();

private:
    HcclResult ThreadIdCreate(uint32_t &threadId);

    static HcclResult InitGroupId();
    static HcclResult GetGroupId();

    bool isDestroyed{false};
    CpuSendInfo sendinfo_;
    std::atomic<bool> break_ = {false};
    std::atomic<bool> isWaiting_ = {false};

    static u32 groupId_;
    static u32 eventId_;
    static u32 initialThreadId_;
};

}

#endif // ESCHED_NOTIFY_H
